/******************************************************************************
 * Copyright 2023 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include "modules/canbus_vehicle/x3epro/protocol/ads_lampcommand_309.h"
#include "modules/drivers/canbus/common/byte.h"

namespace apollo {
namespace canbus {
namespace x3epro {

using ::apollo::drivers::canbus::Byte;

const int32_t Adslampcommand309::ID = 0x309;

// public
Adslampcommand309::Adslampcommand309() { Reset(); }

uint32_t Adslampcommand309::GetPeriod() const {
  // TODO(All) :  modify every protocol's period manually
  static const uint32_t PERIOD = 100 * 1000;
  return PERIOD;
}

void Adslampcommand309::Parse(const std::uint8_t* bytes, int32_t length,
                         X3epro* chassis) const {
  chassis->mutable_ads_lampcommand_309()->set_orin_cleanerrelay(orin_cleanerrelay(bytes, length));
  chassis->mutable_ads_lampcommand_309()->set_turn_right_light_command(turn_right_light_command(bytes, length));
  chassis->mutable_ads_lampcommand_309()->set_turn_left_light_command(turn_left_light_command(bytes, length));
  chassis->mutable_ads_lampcommand_309()->set_horn_command(horn_command(bytes, length));
  chassis->mutable_ads_lampcommand_309()->set_daylamp_command(daylamp_command(bytes, length));
  chassis->mutable_ads_lampcommand_309()->set_headbeam_command(headbeam_command(bytes, length));
  chassis->mutable_ads_lampcommand_309()->set_positionlamp_command(positionlamp_command(bytes, length));
  chassis->mutable_ads_lampcommand_309()->set_froglamp_command(froglamp_command(bytes, length));
  chassis->mutable_ads_lampcommand_309()->set_alivecounter(alivecounter(bytes, length));
  chassis->mutable_ads_lampcommand_309()->set_checksum(checksum(bytes, length));
}

void Adslampcommand309::UpdateData(uint8_t* data) {
  set_p_orin_cleanerrelay(data, orin_cleanerrelay_);
  set_p_turn_right_light_command(data, turn_right_light_command_);
  set_p_turn_left_light_command(data, turn_left_light_command_);
  set_p_horn_command(data, horn_command_);
  set_p_daylamp_command(data, daylamp_command_);
  set_p_headbeam_command(data, headbeam_command_);
  set_p_positionlamp_command(data, positionlamp_command_);
  set_p_froglamp_command(data, froglamp_command_);

  ++alivecounter_;
  alivecounter_ = (alivecounter_) % 16;
  set_p_alivecounter(data, alivecounter_);

  checksum_ =
      data[0] ^ data[1] ^ data[2] ^ data[3] ^ data[4] ^ data[5] ^ data[6];
  set_p_checksum(data, checksum_);
}

void Adslampcommand309::UpdateData_Heartbeat(uint8_t* data) {
  ++alivecounter_;
  alivecounter_ = (alivecounter_) % 16;
  set_p_alivecounter(data, alivecounter_);

  checksum_ =
      data[0] ^ data[1] ^ data[2] ^ data[3] ^ data[4] ^ data[5] ^ data[6];
  set_p_checksum(data, checksum_);
}

void Adslampcommand309::Reset() {
  // TODO(All) :  you should check this manually
  orin_cleanerrelay_ = Ads_lampcommand_309::ORIN_CLEANERRELAY_DISABLE;
  turn_right_light_command_ = Ads_lampcommand_309::TURN_RIGHT_LIGHT_COMMAND_DISABLE;
  turn_left_light_command_ = Ads_lampcommand_309::TURN_LEFT_LIGHT_COMMAND_DISABLE;
  horn_command_ = Ads_lampcommand_309::HORN_COMMAND_DISABLE;
  daylamp_command_ = Ads_lampcommand_309::DAYLAMP_COMMAND_OFF;
  headbeam_command_ = Ads_lampcommand_309::HEADBEAM_COMMAND_OFF;
  positionlamp_command_ = Ads_lampcommand_309::POSITIONLAMP_COMMAND_OFF;
  froglamp_command_ = Ads_lampcommand_309::FROGLAMP_COMMAND_OFF;
  alivecounter_ = 0;
  checksum_ = 0;
}

Adslampcommand309* Adslampcommand309::set_orin_cleanerrelay(
    Ads_lampcommand_309::Orin_cleanerrelayType orin_cleanerrelay) {
  orin_cleanerrelay_ = orin_cleanerrelay;
  return this;
 }

// config detail: {'bit': 0, 'enum': {0: 'ORIN_CLEANERRELAY_DISABLE', 1: 'ORIN_CLEANERRELAY_ENABLE'}, 'is_signed_var': False, 'len': 1, 'name': 'Orin_CleanerRelay', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
void Adslampcommand309::set_p_orin_cleanerrelay(uint8_t* data,
    Ads_lampcommand_309::Orin_cleanerrelayType orin_cleanerrelay) {
  int x = orin_cleanerrelay;

  Byte to_set(data + 0);
  to_set.set_value(x, 0, 1);
}


Adslampcommand309* Adslampcommand309::set_turn_right_light_command(
    Ads_lampcommand_309::Turn_right_light_commandType turn_right_light_command) {
  turn_right_light_command_ = turn_right_light_command;
  return this;
 }

// config detail: {'bit': 8, 'enum': {0: 'TURN_RIGHT_LIGHT_COMMAND_DISABLE', 1: 'TURN_RIGHT_LIGHT_COMMAND_ENABLE'}, 'is_signed_var': False, 'len': 1, 'name': 'Turn_Right_Light_Command', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
void Adslampcommand309::set_p_turn_right_light_command(uint8_t* data,
    Ads_lampcommand_309::Turn_right_light_commandType turn_right_light_command) {
  int x = turn_right_light_command;

  Byte to_set(data + 1);
  to_set.set_value(x, 0, 1);
}


Adslampcommand309* Adslampcommand309::set_turn_left_light_command(
    Ads_lampcommand_309::Turn_left_light_commandType turn_left_light_command) {
  turn_left_light_command_ = turn_left_light_command;
  return this;
 }

// config detail: {'bit': 9, 'enum': {0: 'TURN_LEFT_LIGHT_COMMAND_DISABLE', 1: 'TURN_LEFT_LIGHT_COMMAND_ENABLE'}, 'is_signed_var': False, 'len': 1, 'name': 'Turn_Left_Light_Command', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
void Adslampcommand309::set_p_turn_left_light_command(uint8_t* data,
    Ads_lampcommand_309::Turn_left_light_commandType turn_left_light_command) {
  int x = turn_left_light_command;

  Byte to_set(data + 1);
  to_set.set_value(x, 1, 1);
}


Adslampcommand309* Adslampcommand309::set_horn_command(
    Ads_lampcommand_309::Horn_commandType horn_command) {
  horn_command_ = horn_command;
  return this;
 }

// config detail: {'bit': 10, 'enum': {0: 'HORN_COMMAND_DISABLE', 1: 'HORN_COMMAND_ENABLE'}, 'is_signed_var': False, 'len': 1, 'name': 'Horn_Command', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
void Adslampcommand309::set_p_horn_command(uint8_t* data,
    Ads_lampcommand_309::Horn_commandType horn_command) {
  int x = horn_command;

  Byte to_set(data + 1);
  to_set.set_value(x, 2, 1);
}


Adslampcommand309* Adslampcommand309::set_daylamp_command(
    Ads_lampcommand_309::Daylamp_commandType daylamp_command) {
  daylamp_command_ = daylamp_command;
  return this;
 }

// config detail: {'bit': 11, 'enum': {0: 'DAYLAMP_COMMAND_OFF', 1: 'DAYLAMP_COMMAND_ON'}, 'is_signed_var': False, 'len': 1, 'name': 'DayLamp_Command', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
void Adslampcommand309::set_p_daylamp_command(uint8_t* data,
    Ads_lampcommand_309::Daylamp_commandType daylamp_command) {
  int x = daylamp_command;

  Byte to_set(data + 1);
  to_set.set_value(x, 3, 1);
}


Adslampcommand309* Adslampcommand309::set_headbeam_command(
    Ads_lampcommand_309::Headbeam_commandType headbeam_command) {
  headbeam_command_ = headbeam_command;
  return this;
 }

// config detail: {'bit': 12, 'enum': {0: 'HEADBEAM_COMMAND_OFF', 1: 'HEADBEAM_COMMAND_ON'}, 'is_signed_var': False, 'len': 1, 'name': 'HeadBeam_Command', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
void Adslampcommand309::set_p_headbeam_command(uint8_t* data,
    Ads_lampcommand_309::Headbeam_commandType headbeam_command) {
  int x = headbeam_command;

  Byte to_set(data + 1);
  to_set.set_value(x, 4, 1);
}


Adslampcommand309* Adslampcommand309::set_positionlamp_command(
    Ads_lampcommand_309::Positionlamp_commandType positionlamp_command) {
  positionlamp_command_ = positionlamp_command;
  return this;
 }

// config detail: {'bit': 13, 'enum': {0: 'POSITIONLAMP_COMMAND_OFF', 1: 'POSITIONLAMP_COMMAND_ON'}, 'is_signed_var': False, 'len': 1, 'name': 'PositionLamp_Command', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
void Adslampcommand309::set_p_positionlamp_command(uint8_t* data,
    Ads_lampcommand_309::Positionlamp_commandType positionlamp_command) {
  int x = positionlamp_command;

  Byte to_set(data + 1);
  to_set.set_value(x, 5, 1);
}


Adslampcommand309* Adslampcommand309::set_froglamp_command(
    Ads_lampcommand_309::Froglamp_commandType froglamp_command) {
  froglamp_command_ = froglamp_command;
  return this;
 }

// config detail: {'bit': 14, 'enum': {0: 'FROGLAMP_COMMAND_OFF', 1: 'FROGLAMP_COMMAND_ON'}, 'is_signed_var': False, 'len': 1, 'name': 'FrogLamp_Command', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
void Adslampcommand309::set_p_froglamp_command(uint8_t* data,
    Ads_lampcommand_309::Froglamp_commandType froglamp_command) {
  int x = froglamp_command;

  Byte to_set(data + 1);
  to_set.set_value(x, 6, 1);
}


Adslampcommand309* Adslampcommand309::set_alivecounter(
    int alivecounter) {
  alivecounter_ = alivecounter;
  return this;
 }

// config detail: {'bit': 51, 'is_signed_var': False, 'len': 4, 'name': 'AliveCounter', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'int'}
void Adslampcommand309::set_p_alivecounter(uint8_t* data,
    int alivecounter) {
  alivecounter = ProtocolData::BoundedValue(0, 15, alivecounter);
  int x = alivecounter;

  Byte to_set(data + 6);
  to_set.set_value(x, 0, 4);
}


Adslampcommand309* Adslampcommand309::set_checksum(
    int checksum) {
  checksum_ = checksum;
  return this;
 }

// config detail: {'bit': 63, 'is_signed_var': False, 'len': 8, 'name': 'CheckSum', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'int'}
void Adslampcommand309::set_p_checksum(uint8_t* data,
    int checksum) {
  checksum = ProtocolData::BoundedValue(0, 255, checksum);
  int x = checksum;

  Byte to_set(data + 7);
  to_set.set_value(x, 0, 8);
}


Ads_lampcommand_309::Orin_cleanerrelayType Adslampcommand309::orin_cleanerrelay(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 0);
  int32_t x = t0.get_byte(0, 1);

  Ads_lampcommand_309::Orin_cleanerrelayType ret =  static_cast<Ads_lampcommand_309::Orin_cleanerrelayType>(x);
  return ret;
}

Ads_lampcommand_309::Turn_right_light_commandType Adslampcommand309::turn_right_light_command(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 1);
  int32_t x = t0.get_byte(0, 1);

  Ads_lampcommand_309::Turn_right_light_commandType ret =  static_cast<Ads_lampcommand_309::Turn_right_light_commandType>(x);
  return ret;
}

Ads_lampcommand_309::Turn_left_light_commandType Adslampcommand309::turn_left_light_command(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 1);
  int32_t x = t0.get_byte(1, 1);

  Ads_lampcommand_309::Turn_left_light_commandType ret =  static_cast<Ads_lampcommand_309::Turn_left_light_commandType>(x);
  return ret;
}

Ads_lampcommand_309::Horn_commandType Adslampcommand309::horn_command(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 1);
  int32_t x = t0.get_byte(2, 1);

  Ads_lampcommand_309::Horn_commandType ret =  static_cast<Ads_lampcommand_309::Horn_commandType>(x);
  return ret;
}

Ads_lampcommand_309::Daylamp_commandType Adslampcommand309::daylamp_command(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 1);
  int32_t x = t0.get_byte(3, 1);

  Ads_lampcommand_309::Daylamp_commandType ret =  static_cast<Ads_lampcommand_309::Daylamp_commandType>(x);
  return ret;
}

Ads_lampcommand_309::Headbeam_commandType Adslampcommand309::headbeam_command(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 1);
  int32_t x = t0.get_byte(4, 1);

  Ads_lampcommand_309::Headbeam_commandType ret =  static_cast<Ads_lampcommand_309::Headbeam_commandType>(x);
  return ret;
}

Ads_lampcommand_309::Positionlamp_commandType Adslampcommand309::positionlamp_command(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 1);
  int32_t x = t0.get_byte(5, 1);

  Ads_lampcommand_309::Positionlamp_commandType ret =  static_cast<Ads_lampcommand_309::Positionlamp_commandType>(x);
  return ret;
}

Ads_lampcommand_309::Froglamp_commandType Adslampcommand309::froglamp_command(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 1);
  int32_t x = t0.get_byte(6, 1);

  Ads_lampcommand_309::Froglamp_commandType ret =  static_cast<Ads_lampcommand_309::Froglamp_commandType>(x);
  return ret;
}

int Adslampcommand309::alivecounter(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 6);
  int32_t x = t0.get_byte(0, 4);

  int ret = x;
  return ret;
}

int Adslampcommand309::checksum(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 7);
  int32_t x = t0.get_byte(0, 8);

  int ret = x;
  return ret;
}
}  // namespace x3epro
}  // namespace canbus
}  // namespace apollo
